473,440 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,440 software developers and data experts.

Speed of switch/case vs hash table for parsing XML

_DS
I'm currently using a switch with about 50 case statements in a
stretch of code that's parsing XML attributes. Each case is a string.
I'm told that switch statements will actually use hash tables when
number of cases is around 10 or more, so I haven't changed the code.
Just wondering if anyone knows about how that's structured internally.
It does seem a bit slow.

Feb 11 '06 #1
5 3900
_DS <_D*@nomail.com> wrote:
I'm currently using a switch with about 50 case statements in a
stretch of code that's parsing XML attributes. Each case is a string.
I'm told that switch statements will actually use hash tables when
number of cases is around 10 or more, so I haven't changed the code.
Just wondering if anyone knows about how that's structured internally.
It does seem a bit slow.


What exactly do you mean by the last sentence? Do you mean you've
implemented some code and it's working slowly, or that it sounds like a
slow way of doing things?

I suspect if you're really seeing something working slowly, it won't be
due to the switch statement. I very much doubt that that will be the
bottleneck.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 11 '06 #2
I'm almost certain that the C# compiler will not implement the select
construct with a Hashtable. You can easily check by running ildasm on
the assembly. Either way 50 case statements is an awful lot. If
nothing else I recommend refactoring the code to gain readability.

Brian

_DS wrote:
I'm currently using a switch with about 50 case statements in a
stretch of code that's parsing XML attributes. Each case is a string.
I'm told that switch statements will actually use hash tables when
number of cases is around 10 or more, so I haven't changed the code.
Just wondering if anyone knows about how that's structured internally.
It does seem a bit slow.


Feb 11 '06 #3
Brian Gideon <br*********@yahoo.com> wrote:
I'm almost certain that the C# compiler will not implement the select
construct with a Hashtable. You can easily check by running ildasm on
the assembly. Either way 50 case statements is an awful lot. If
nothing else I recommend refactoring the code to gain readability.


It's easy to verify that at least in the case of strings, the C#
compiler does indeed use a hash table. Compile the following and then
disassemble it:

class Test
{
static void Main (string[] args)
{
int i=0;
switch (args[0])
{
case "1":
case "2":
case "3":
case "4":
case "5":
i=1;
break;
case "6":
case "7":
case "8":
case "9":
case "10":
i=2;
break;
}
i++;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 11 '06 #4
Jon,

That's actually pretty cool. I suppose if it does it for strings it
would do it for others as well. I never would have thought...

Brian

Jon wrote:
Brian Gideon <br*********@yahoo.com> wrote:
I'm almost certain that the C# compiler will not implement the select
construct with a Hashtable. You can easily check by running ildasm on
the assembly. Either way 50 case statements is an awful lot. If
nothing else I recommend refactoring the code to gain readability.


It's easy to verify that at least in the case of strings, the C#
compiler does indeed use a hash table. Compile the following and then
disassemble it:

class Test
{
static void Main (string[] args)
{
int i=0;
switch (args[0])
{
case "1":
case "2":
case "3":
case "4":
case "5":
i=1;
break;
case "6":
case "7":
case "8":
case "9":
case "10":
i=2;
break;
}
i++;
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Feb 11 '06 #5
Brian Gideon <br*********@yahoo.com> wrote:
That's actually pretty cool. I suppose if it does it for strings it
would do it for others as well. I never would have thought...


It doesn't actually need to do it for other types. The thing is that
the IL switch instruction only supports integral types, so the C#
compiler needs to "fake it" for string.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
24
by: | last post by:
Hi, I need to read a big CSV file, where different fields should be converted to different types, such as int, double, datetime, SqlMoney, etc. I have an array, which describes the fields and...
8
by: _eddie | last post by:
Is there a good way to code a switch/case-type construct for maximal speed? The goal is to parse text key/value pairs. IOW: // key = "Text of some kind" // value = "Value Text" string...
11
by: Sezai YILMAZ | last post by:
Hello I need high throughput while inserting into PostgreSQL. Because of that I did some PostgreSQL insert performance tests. ------------------------------------------------------------ --...
6
by: jao | last post by:
My company has a product in beta which uses Postgres 7.4.3. We expect to have a code freeze for our 1.0 product in March 2005. I'd really like to use Postgres 8.x in our 1.0 product. We're...
11
by: ME | last post by:
In C# the following code generates a compiler error ("A constant value is expected"): public void Test(string value) { switch (value) { case SimpleEnum.One.ToString(): MessageBox.Show("Test...
7
by: Michael | last post by:
I'm writing an application that decodes a file containing binary records. Each record is a particular event type. Each record is translated into ASCII and then written to a file. Each file contains...
1
by: MariuszC | last post by:
Hello, I have used some library where is defined enum like: typedef enum Enum1 { ENUM1_1, ENUM1_2, ENUM1_3, ENUM1_4, ENUM1_5 }; In my code I have defined: typedef enum Enum2 { ENUM2_1,...
21
by: aaron80v | last post by:
Hi, A simple question: In ANSI-C, how to specify a string as the expression for switch statement. eg switch (mystr) { case "ABC" : bleh... break;
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.